package com.zkh360.service.message.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.zkh360.service.message.constant.Constants;
import com.zkh360.service.message.constant.ErrorCode;
import com.zkh360.service.message.controller.base.BaseController;
import com.zkh360.service.message.exceptions.BusinessException;
import com.zkh360.service.message.model.BaseResponse;
import com.zkh360.service.message.model.Pager;
import com.zkh360.service.message.model.SysUserVO;
import com.zkh360.service.message.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户controller
 * 
 * @author chuanxi.zhao
 *
 */
@Controller
@Slf4j
public class UserConrtroller extends BaseController {

	@Autowired
	private UserService userService;

	@GetMapping("/users")
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public String users(Integer currentPage, Model model) {
		log.info("获取用户信息");
		Pager pager = new Pager();
		pager.setCurrentPage(currentPage);
		pager.setPageSize(Constants.PAGE_SIZE);

		SysUserVO sysUsers = new SysUserVO();
		sysUsers.setUser_account(getUserName());
		List<SysUserVO> sysUserList = userService.getUsers(sysUsers, pager);
		// 设置分页信息
		int totalCount = userService.getUsersCount(sysUsers);
		setPager(currentPage, totalCount, pager);
		model.addAttribute("pager", pager);
		// 前端返回
		model.addAttribute("users", sysUserList);
		return "users";
	}

	/**
	 * 新增用户信息（主要是URL地址）
	 * 
	 * @throws IOException 读写异常
	 */
	@PostMapping("/user/add")
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public void addUser(@RequestBody SysUserVO sysUserVo, HttpServletResponse response) throws IOException {
		log.info("添加用户信息");
		// 前端返回
		BaseResponse<String> result = BaseResponse.sucess(ErrorCode.CODE_SUCESS, "success", "添加成功");
		try {
			userService.addUser(sysUserVo);
		} catch (BusinessException e) {
			log.error("添加用户信息异常", e.getErrorMsg());
			result = BaseResponse.fail(e.getErrorCode(), "fail", e.getErrorMsg());
		}

		writeJSON(response, result);
	}

	/**
	 * 删除用户信息
	 * 
	 * @throws IOException IO异常
	 */
	@GetMapping("/user/delete")
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public void deleteUser(Long userId, HttpServletResponse response) throws IOException {
		log.info("删除用户信息详情");
		BaseResponse<String> result = BaseResponse.sucess(ErrorCode.CODE_SUCESS, "success", "删除成功");

		try {
			userService.deleteUser(userId);
		} catch (BusinessException e) {
			log.error("删除用户信息异常", e.getErrorMsg());
			result = BaseResponse.fail(e.getErrorCode(), "fail", e.getErrorMsg());
		}

		writeJSON(response, result);
	}

	/**
	 * 修改用户信息
	 * 
	 * @throws IOException 读写异常
	 */
	@PostMapping("/user/edit")
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public void editUser(@RequestBody SysUserVO sysUserVo, HttpServletResponse response) throws IOException {
		log.info("修改用户信息");
		// 前端返回
		BaseResponse<String> result = BaseResponse.sucess(ErrorCode.CODE_SUCESS, "success", "添加成功");
		try {
			userService.editUser(sysUserVo);
		} catch (BusinessException e) {
			log.error("添加用户信息异常", e.getErrorMsg());
			result = BaseResponse.fail(e.getErrorCode(), "fail", e.getErrorMsg());
		}

		writeJSON(response, result);
	}

	/**
	 * 获取用户信息 主要是点击修改按钮获取详情
	 * 
	 * @throws IOException IO异常
	 */
	@GetMapping("/user")
	public void getUser(Long userId, HttpServletResponse response) throws IOException {
		log.info("获取用户信息详情");
		SysUserVO sysRoleVo = new SysUserVO();
		sysRoleVo.setUser_id(userId);
		List<SysUserVO> sysRoleList = userService.getUsers(sysRoleVo);
		BaseResponse<SysUserVO> result = BaseResponse.sucess(ErrorCode.CODE_SUCESS, "success", null);

		if (sysRoleList.size() == 0) {
			result = BaseResponse.fail(ErrorCode.CODE_FAILURE, "查询不到该用户信息！", null);
		} else if (sysRoleList.size() == 1) {
			result.setResult(sysRoleList.get(0));
		} else {
			result = BaseResponse.fail(ErrorCode.CODE_FAILURE, "查询到多个用户信息！", null);
		}

		writeJSON(response, result);
	}

}
